

	*****************************************************
	* (1.0) Run regressions, output items needed for tables and 
	* 		Wild bootstrap - based on code bs_example.do 
	* 		published by Colin Cameron, accessed 2018
	*****************************************************
	* Generate the residuals, predicted y, and save coef / tstats from main regressions 
	set seed 8675309
	
	***************************************************
	* (1.1) Postnatal Selection Second Birth
	***************************************************	

		xi: reg cmale noson period_2 period_3 period2_noson period3_noson   ///
			$controls1 $controls2 i.FE_t i.FE_prov if year!=1967 & year!=1968 & order == 2 , cluster(prov)

			* Hypothesis tests: Interested in the effect of the period and 
			* lack of a son on probability of having a son, relative to births 
			* in the same period at same parity
			
				* Note births with a son in the same period is the reference group
				* Period: Pre LLF; Previous son: No
				lincom noson + 0
				local est_p2_ns = r(estimate)
									
				* Period: Early LLF; Previous son: No
				lincom noson + period2_noson
				local est_e2_ns = r(estimate)
								
				* Period: Late LLF; Previous son: No
				lincom noson + period3_noson
				local est_l2_ns = r(estimate)
									
			mat pre_2nd 	= ., `est_p2_ns'
			mat early_2nd 	= ., `est_e2_ns'
			mat late_2nd 	= ., `est_l2_ns'

	tab prov, gen(prov_)
	local combo = 1
	forval i = 	1/28 {
		forval j = 1/28 {  
	
		xi: reg cmale noson period_2 period_3 period2_noson period3_noson   ///
			$controls1 $controls2 i.FE_t i.FE_prov ///
			if year!=1967 & year!=1968 & order == 2 & prov_`i' == 0 & prov_`j' == 0, cluster(prov)
			
				* Period: Pre LLF; Previous son: No
				lincom noson + 0
				local est_p2_ns = r(estimate)
									
				* Period: Early LLF; Previous son: No
				lincom noson + period2_noson
				local est_e2_ns = r(estimate)
								
				* Period: Late LLF; Previous son: No
				lincom noson + period3_noson
				local est_l2_ns = r(estimate)
	
			mat temp1 = `combo', `est_p2_ns'
			mat temp2 = `combo', `est_e2_ns'
			mat temp3 = `combo', `est_l2_ns'

			mat pre_2nd 	= pre_2nd 	\ temp1
			mat early_2nd 	= early_2nd \ temp2
			mat late_2nd 	= late_2nd 	\ temp3
			
			local combo = `++combo' 
		}
	}
			
			
	***************************************************
	* (1.2) Postnatal Selection Third + Births
	***************************************************
					
			xi: reg cmale noson period_2 period_3 period2_noson period3_noson   ///
				$controls1 $controls2 i.FE_t i.FE_prov ///
				if year!=1967 & year!=1968 & order >= 3 & years_since_LLF<8 , cluster(prov)

				* Period: Pre LLF; Previous son: No
				lincom noson + 0
				local est_p3_ns = r(estimate)
								
				* Period: Early LLF; Previous son: No
				lincom noson + period2_noson
				local est_e3_ns = r(estimate)

				* Period: Late LLF; Previous son: No
				lincom noson + period3_noson
				local est_l3_ns = r(estimate)
				
			mat pre_3rd 	= ., `est_p3_ns'
			mat early_3rd 	= ., `est_e3_ns'
			mat late_3rd 	= ., `est_l3_ns'

	local combo = 1
	forval i = 	1/28 {
		forval j = 1/28 {  
	
		xi: reg cmale noson period_2 period_3 period2_noson period3_noson   ///
			$controls1 $controls2 i.FE_t i.FE_prov ///
			if year!=1967 & year!=1968 & order >= 3 & years_since_LLF<8 & prov_`i' == 0 & prov_`j' == 0, cluster(prov)
			
				* Period: Pre LLF; Previous son: No
				lincom noson + 0
				local est_p3_ns = r(estimate)
									
				* Period: Early LLF; Previous son: No
				lincom noson + period2_noson
				local est_e3_ns = r(estimate)
								
				* Period: Late LLF; Previous son: No
				lincom noson + period3_noson
				local est_l3_ns = r(estimate)
	
			mat temp1 = `combo', `est_p3_ns'
			mat temp2 = `combo', `est_e3_ns'
			mat temp3 = `combo', `est_l3_ns'

			mat pre_3rd 	= pre_3rd 	\ temp1
			mat early_3rd 	= early_3rd \ temp2
			mat late_3rd 	= late_3rd 	\ temp3
			
			local combo = `++combo' 
		}
	}		
	
	mat leave2out_results = pre_2nd, early_2nd, late_2nd, pre_3rd, early_3rd, late_3rd
	mat colnames leave2out_results = "combo2p" "coef_pre2nd" "combo2e" "coef_early2nd" "combo2l" "coef_late2nd" "combo3p" "coef_pre3rd" "combo3e" "coef_early3rd" "combo3l" "coef_late3rd"
	
	***************************************************
	* (2.0) Postnatal Selection Third + Births
	***************************************************

	clear 
	svmat leave2out_results, names(col)
		sum coef_pre2nd 
		local main = r(mean)
		sum coef_pre2nd  if combo2p==321
		local test = r(mean)
		
		twoway  ///
				(kdensity coef_pre2nd ) ///			
				, xline(`test', lcolor(red) lpattern(dash)) xline(.002, lcolor(red)) ///
				   xlab(-.01(.005).05)  ///
				xtitle("Probability of a male birth" "Among couples with no previous sons") ytitle("")  ///
				title("Second Parity Births, Pre-LLF") note("Solid line shows main estimate, dash shows estimates excluding Fujian and Jiangxi provinces")
				
		graph export `"$d_fig\Fig_A17a.png"', replace	

		
		sum coef_early2nd 
		local main = r(mean)
		sum coef_early2nd  if combo2e==321
		local test = r(mean)
		
		twoway  ///
				(kdensity coef_early2nd ) ///			
				, xline(`test', lcolor(red) lpattern(dash)) xline(.009, lcolor(red) )  xlab(-.01(.005).05)  ///
				xtitle("Probability of a male birth" "Among couples with no previous sons") ytitle("")  ///
				legend(off) title("Second Parity Births, Early LLF") note("Solid line shows main estimate, dash shows estimates excluding Fujian and Jiangxi provinces")		
				
		graph export `"$d_fig\Fig_A17c.png"', replace	
		
		sum coef_late2nd 
		local main = r(mean)
		sum coef_late2nd  if combo2l==321
		local test = r(mean)
		
		twoway  ///
				(kdensity coef_late2nd ) ///			
				, xline(`test', lcolor(red) lpattern(dash)) xline(.006, lcolor(red) )  xlab(-.01(.005).05)  ///
				xtitle("Probability of a male birth" "Among couples with no previous sons") ytitle("")  ///
				legend(off) title("Second Parity Births, Late LLF")	note("Solid line shows main estimate, dash shows estimates excluding Fujian and Jiangxi provinces")			
				
		graph export `"$d_fig\Fig_A17e.png"', replace	
			
		sum coef_pre3rd 
		local main = r(mean)
		sum coef_pre3rd  if combo3p==321
		local test = r(mean)
		
		twoway  ///
				(kdensity coef_pre3rd ) ///			
				, xline(`test', lcolor(red) lpattern(dash)) xline(.007, lcolor(red) )  xlab(-.01(.005).05)   ///
				xtitle("Probability of a male birth" "Among couples with no previous sons") ytitle("")  ///
				legend(off) title("Third and Higher Parity Births, Pre LLF") note("Solid line shows main estimate, dash shows estimates excluding Fujian and Jiangxi provinces")				
				
		graph export `"$d_fig\Fig_A17b.png"', replace	
			
		sum coef_early3rd 
		local main = r(mean)
		sum coef_early3rd  if combo3e==321
		local test = r(mean)
		
		twoway  ///
				(kdensity coef_early3rd ) ///			
				, xline(`test', lcolor(red) lpattern(dash)) xline(.000, lcolor(red) )  xlab(-.01(.005).05)   ///
				xtitle("Probability of a male birth" "Among couples with no previous sons") ytitle("")  ///
				legend(off) title("Third and Higher Parity Births, Early LLF")	note("Solid line shows main estimate, dash shows estimates excluding Fujian and Jiangxi provinces")			
				
		graph export `"$d_fig\Fig_A17d.png"', replace	
		
		sum coef_late3rd 
		local main = r(mean)
		sum coef_late3rd  if combo3l==321
		local test = r(mean)
		
		twoway  ///
				(kdensity coef_late3rd ) ///			
				, xline(`test', lcolor(red) lpattern(dash)) xline(.031, lcolor(red) )  xlab(-.01(.005).05)   ///
				xtitle("Probability of a male birth" "Among couples with no previous sons") ytitle("")  ///
				legend(off) title("Third and Higher Parity Births, Late LLF")	note("Solid line shows main estimate, dash shows estimates excluding Fujian and Jiangxi provinces")			
				
		graph export `"$d_fig\Fig_A17f.png"', replace	
	
